﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Security;
using System.Security.Principal;
using CNdotnet.Ezsocio.Core.Validations;
using CNdotnet.Ezsocio.DependenceInjection;

namespace CNdotnet.Ezsocio.Core.Web
{
    /// <summary>
    /// The base class of controller
    /// </summary>
    public abstract class BaseController : Controller
    {
        #region Validations

        /// <summary>
        /// Validat entity
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        /// <param name="entity">Entity reference</param>
        /// <returns>validation result</returns>
        public virtual bool Validat<TEntity>(TEntity entity)
            where TEntity : IValidated
        {
            bool flag = true;
            if (!entity.Validate<TEntity>())
            {
                ViewData.ModelState.AttachValidationErrors(entity.ValidationResults);
                
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Service Manager

        /// <summary>
        /// Loads the service.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <returns></returns>
        public TService LoadService<TService>()
        {
            IContainerContext container = ContainerManager.GetContainer("serviceContainer");
            return container.Resolve<TService>();
        }

        #endregion

        #region Override Methods

        /// <summary>
        /// Called when action executing.
        /// </summary>
        /// <remarks>When current authentication is not FormAuthentication, throw a exception</remarks>
        /// <param name="filterContext">The filter context.</param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.User.Identity is WindowsIdentity)
            {
                throw new InvalidOperationException("Windows authentication is not supported.");
            }
            base.OnActionExecuting(filterContext);
        }

        #endregion
    }
}
